package com.example.eduservice.controller;


import com.example.commonutils.R;
import com.example.eduservice.client.VodClient;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * <p>
 * 课程视频 前端控制器
 * 课程小节
 * </p>
 *
 * @author testjava
 * @since 2023-11-04
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    //注入VodClient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        System.out.println("验证前端传过来的是否免费是什么样的：" + eduVideo);
        eduVideoService.save(eduVideo);
        return R.ok();
    }
    //删除小节 删除对应的阿里云的视频
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id获取视频id，调用方法实现视频删除
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id，远程调用实现视频删除
            vodClient.removeAlyVideo(videoSourceId);
        }
        //删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }
    //先回显信息
    @GetMapping("getVideo/{videoId}")
    public R getVideo(@PathVariable String videoId){
        System.out.println("!!!!!!!!" + videoId);
        EduVideo video = eduVideoService.getById(videoId);
        return R.ok().data("video",video);
    }
    //修改小节
    @PostMapping("updateVideos")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

}

